<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="600" append-to-body destroy-on-close @closed="$emit('closed')">
		<el-container>
			<el-main class="nopadding">
				<el-scrollbar>
					<el-row class="drawer-table">
						<el-col :span="24">
							<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="80px">
								<el-form-item label="产品" prop="productId">
									<el-input v-model="form.productId" placeholder="请输入产品" clearable></el-input>
								</el-form-item>
								<el-form-item label="商家id" prop="departmentId">
									<el-input v-model="form.departmentId" placeholder="请输入商家id" clearable></el-input>
								</el-form-item>
								<el-form-item label="系统栏目" prop="sysCategoryId">
									<el-input v-model="form.sysCategoryId" placeholder="请输入系统栏目" clearable></el-input>
								</el-form-item>
								<el-form-item label="系统栏目路径" prop="sysCategoryPath">
									<el-input v-model="form.sysCategoryPath" placeholder="请输入系统栏目路径" clearable></el-input>
								</el-form-item>
								<el-form-item label="发布 区域全路径" prop="regionPath">
									<el-input v-model="form.regionPath" placeholder="请输入发布 区域全路径" clearable></el-input>
								</el-form-item>
								<el-form-item label="1新品 2二手货 3积分兑换  8自营店新品" prop="proType">
									<el-input v-model="form.proType" placeholder="请输入1新品 2二手货 3积分兑换  8自营店新品" clearable></el-input>
								</el-form-item>
								<el-form-item label="产品编码 国际化产品此编码相同" prop="productCode">
									<el-input v-model="form.productCode" placeholder="请输入产品编码 国际化产品此编码相同" clearable></el-input>
								</el-form-item>
								<el-form-item label="产品名称 最长35个字符" prop="productName">
									<el-input v-model="form.productName" placeholder="请输入产品名称 最长35个字符" clearable></el-input>
								</el-form-item>
								<el-form-item label="宝贝卖点 150字" prop="shortDesc">
									<el-input v-model="form.shortDesc" placeholder="请输入宝贝卖点 150字" clearable></el-input>
								</el-form-item>
								<el-form-item label="详细描述" prop="fullDesc">
									<el-input v-model="form.fullDesc" placeholder="请输入详细描述" clearable></el-input>
								</el-form-item>
								<el-form-item label="搜索关键字" prop="searchKey">
									<el-input v-model="form.searchKey" placeholder="请输入搜索关键字" clearable></el-input>
								</el-form-item>
								<el-form-item label="商品条形码" prop="barCode">
									<el-input v-model="form.barCode" placeholder="请输入商品条形码" clearable></el-input>
								</el-form-item>
								<el-form-item label="市场价格" prop="listPrice">
									<el-input v-model="form.listPrice" placeholder="请输入市场价格" clearable></el-input>
								</el-form-item>
								<el-form-item label="价格 一口价" prop="price">
									<el-input v-model="form.price" placeholder="请输入价格 一口价" clearable></el-input>
								</el-form-item>
								<el-form-item label="成本" prop="costPrice">
									<el-input v-model="form.costPrice" placeholder="请输入成本" clearable></el-input>
								</el-form-item>
								<el-form-item label="产品页面模板" prop="templatePath">
									<el-input v-model="form.templatePath" placeholder="请输入产品页面模板" clearable></el-input>
								</el-form-item>
								<el-form-item label="促销状态 0无促销 1有促销" prop="saleStatus">
									<el-input v-model="form.saleStatus" placeholder="请输入促销状态 0无促销 1有促销" clearable></el-input>
								</el-form-item>
								<el-form-item label="采购地路径" prop="fromPath">
									<el-input v-model="form.fromPath" placeholder="请输入采购地路径" clearable></el-input>
								</el-form-item>
								<el-form-item label="会员等级 拥有高于该等级的用户才能购买" prop="membershipLevel">
									<el-input v-model="form.membershipLevel" placeholder="请输入会员等级 拥有高于该等级的用户才能购买" clearable></el-input>
								</el-form-item>
								<el-form-item label="橱窗推荐 1是 0否" prop="isPush">
									<el-input v-model="form.isPush" placeholder="请输入橱窗推荐 1是 0否" clearable></el-input>
								</el-form-item>
								<el-form-item label="排序降序" prop="seq">
									<el-input v-model="form.seq" placeholder="请输入排序降序" clearable></el-input>
								</el-form-item>
								<el-form-item label="购买竞价排序" prop="buySeq">
									<el-input v-model="form.buySeq" placeholder="请输入购买竞价排序" clearable></el-input>
								</el-form-item>
								<el-form-item label="默认图片" prop="defImg">
									<el-input v-model="form.defImg" placeholder="请输入默认图片" clearable></el-input>
								</el-form-item>
								<el-form-item label="1已发布 0库存临时 2待审核发布 3审核不通过" prop="status">
									<el-input v-model="form.status" placeholder="请输入1已发布 0库存临时 2待审核发布 3审核不通过" clearable></el-input>
								</el-form-item>
                            </el-form>
						</el-col>
					</el-row>
				</el-scrollbar>
			</el-main>
			<el-footer>
				<el-button v-if="mode!=='show'" :loading="isSaveing" @click="submit()" type="primary" size="small">保 存</el-button>
				<el-button size="small" @click="visible=false">取 消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>
<script>
	import productAPI from '@/api/product/product';
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					mode: ''
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					productId: "",
					departmentId: "",
					sysCategoryId: "",
					sysCategoryPath: "",
					regionPath: "",
					proType: "",
					productCode: "",
					productName: "",
					shortDesc: "",
					fullDesc: "",
					searchKey: "",
					barCode: "",
					listPrice: "",
					price: "",
					costPrice: "",
					templatePath: "",
					saleStatus: "",
					fromPath: "",
					membershipLevel: "",
					isPush: "",
					seq: "",
					buySeq: "",
					defImg: "",
					status: "",
				},
				//验证规则
				rules: {
					userName: [
						{required: true, message: '请输入名称'}
					],
					userAge: [
						{pattern: /^[0-9]*$/, message: '年龄需为数字', trigger: 'blur'}
					],
					price: [
						{pattern: /^\d+(\.\d+)?$/, message: '价格需为数字', trigger: 'blur'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if(valid){
						this.isSaveing = true;
						var res = await productAPI.product.save.post(this.form);
						this.isSaveing = false;
						if(res.code === 2000){
							this.$emit('success', this.form, this.mode);
							this.visible = false;
							this.$message.success(res.message);
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				});
			},
			//表单注入数据
			setData(data){
				this.form.productId = data.productId;
				this.form.departmentId = data.departmentId;
				this.form.sysCategoryId = data.sysCategoryId;
				this.form.sysCategoryPath = data.sysCategoryPath;
				this.form.regionPath = data.regionPath;
				this.form.proType = data.proType;
				this.form.productCode = data.productCode;
				this.form.productName = data.productName;
				this.form.shortDesc = data.shortDesc;
				this.form.fullDesc = data.fullDesc;
				this.form.searchKey = data.searchKey;
				this.form.barCode = data.barCode;
				this.form.listPrice = data.listPrice;
				this.form.price = data.price;
				this.form.costPrice = data.costPrice;
				this.form.templatePath = data.templatePath;
				this.form.saleStatus = data.saleStatus;
				this.form.fromPath = data.fromPath;
				this.form.membershipLevel = data.membershipLevel;
				this.form.isPush = data.isPush;
				this.form.seq = data.seq;
				this.form.buySeq = data.buySeq;
				this.form.defImg = data.defImg;
				this.form.status = data.status;
			}
		}
	}
</script>
<style>
</style>
